import { Feature } from "ol";
import { createFeature } from "../create/createFeature";
import { createPointGeometry } from "../create/createGeometry";
import {
  createVectorLayer,
  createHeatmapLayer as heatmapLayer,
} from "../create/createLayer";
import { createSource } from "../create/createSource";
import { transform4326to3857 } from "../utils/transform";
import { Coordinate } from "ol/coordinate";
import { Heatmap, Layer } from "ol/layer";
import { GeoJSON } from "ol/format";
import { VectorSourceEvent } from "ol/source/Vector";
import { FeatureLike } from "ol/Feature";
import { Style } from "ol/style";
import { AreaStyleOptions, createAreaStyle } from "../styles/areaStyle";
import { StyleLike } from "ol/style/Style";

export interface AreaStyle extends AreaStyleOptions {
  name?: string;
}

export interface CreateAreaOptions {
  style?: AreaStyle | ((params: AreaStyleParams) => AreaStyle);
}

export interface AreaData {
  adcode: string;
  isFullData?: boolean;
  level?: "_city" | "_district";
}
export interface AreaStyleParams {
  center: number[];
  adcode: number;
  name: string;
  level: string;
  parent: {
    adcode: number;
  };
  acroutes: number[];
  centroid: number[];
  childrenNum: number;
  subFeatureIndex: number;
}

export function createAreaLayer<T extends AreaData>(
  data: T[],
  options?: CreateAreaOptions
) {
  const layers: Layer[] = [];
  data.forEach((item) => {
    const { adcode, isFullData, level = "" } = item;

    /*  const isCountry = adcode === "100000";
    const isProvince = adcode.endsWith("0000");
    const isCity = adcode.endsWith("00");
    const isDistrict = !adcode.endsWith("00"); */
    if (adcode !== undefined) {
      let url = `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}${
        isFullData ? "_full" : ""
      }${level}.json`;
      const source = createSource({
        url,
        format: new GeoJSON(),
      });
      source.on(
        "featuresloadend",
        function (event: VectorSourceEvent<FeatureLike>) {
          event.features?.forEach(function (feature) {
            const center = feature.get("center");
            const adcode = feature.get("adcode");
            const name = feature.get("name");
            const level = feature.get("level");
            const parent = feature.get("parent");
            const acroutes = feature.get("acroutes");
            const centroid = feature.get("centroid");
            const childrenNum = feature.get("childrenNum");
            const subFeatureIndex = feature.get("subFeatureIndex");

            let styleParams: AreaStyle | null = null;

            if (typeof options?.style === "function") {
              styleParams = options.style({
                center,
                adcode,
                name,
                level,
                parent,
                acroutes,
                centroid,
                childrenNum,
                subFeatureIndex,
              });
            } else if (typeof options?.style === "object") {
              styleParams = options.style;
            }

            if (styleParams) {
              // console.log(s.name);

              const setStyleParams = createAreaStyle(styleParams);
              if (setStyleParams && setStyleParams instanceof Style) {
                const textOptions = setStyleParams.getText()!;
                console.log(styleParams);
                
                if (styleParams?.name || name) {
                  textOptions.setText(styleParams.name || name);
                }
              }
              (feature as Feature).setStyle(setStyleParams);
            }
          });
        }
      );

      const layer = createVectorLayer({
        source,
      });

      layers.push(layer);
    }
  });

  return layers;
}
